<template>
  <div>
    <div class="header">
      <el-button
        type="primary"
        @click="dialogFormVisible = true"
        class="add"
        size="small"
        >新增
      </el-button>
      <div class="search">
        <el-input
          v-model="searchnumber"
          placeholder="请输入学号"
          size="small"
          class="inputnum"
        ></el-input>
        <el-input
          v-model="searchclass"
          placeholder="请输入班级"
          size="small"
          class="inputclass"
        ></el-input>
        <el-button
          type="primary"
          class="searchbutton"
          size="small"
          @click="searchnum"
          >查询
        </el-button>
        <el-button type="primary" class="clear" size="small" @click="clear"
          >清空
        </el-button>
      </div>
    </div>
    <el-table :data="memberlist" style="width: 100%" stripe border  :default-sort = "{prop: 'date', order: 'descending'}">
      <el-table-column label="姓名" width="260" prop="name">
      </el-table-column>
      <el-table-column label="班级" width="260" sortable prop="class">
      </el-table-column>
      <el-table-column label="学号" width="260" sortable prop="number">
      </el-table-column> 
      <el-table-column label="所属社团" width="260" sortable prop="club">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增 -->
    <el-dialog title="新增学生" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" :label-width="formLabelWidth">
              <el-input
                v-model="form.name"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="班级" :label-width="formLabelWidth">
              <el-select v-model="form.class" placeholder="">
                <el-option
                  v-for="item in classoptions"
                  :key="item.id"
                  :label="item.class"
                  :value="item.class"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学号" :label-width="formLabelWidth">
              <el-input v-model="form.number" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属社团" :label-width="formLabelWidth">
              <el-select v-model="form.club" placeholder="">
                <el-option
                  v-for="item in cluboptions"
                  :key="item.id"
                  :label="item.clubname"
                  :value="item.clubname"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="密码" :label-width="formLabelWidth">
              <el-input v-model="form.password" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClub">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改学生" :visible.sync="editFormVisible" width="40%">
      <el-form :model="form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" :label-width="formLabelWidth">
              <el-input
                v-model="editform.name"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="班级" :label-width="formLabelWidth">
              <el-select v-model="editform.class" placeholder="">
                <el-option
                  v-for="item in classoptions"
                  :key="item.id"
                  :label="item.class"
                  :value="item.class"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学号" :label-width="formLabelWidth">
              <el-input v-model="editform.number" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属社团" :label-width="formLabelWidth">
              <el-select v-model="editform.club" placeholder="">
                <el-option
                  v-for="item in cluboptions"
                  :key="item.id"
                  :label="item.clubname"
                  :value="item.clubname"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="密码" :label-width="formLabelWidth">
              <el-input
                v-model="editform.password"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClub">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { v4 as uuidv4 } from "uuid";
export default {
  name: "MyMember",
  mounted() {
    this.all();
    axios
      .get("http://127.0.0.1/classlist_yang/all")
      .then((res) => {
        // console.log(res.data);
        this.classoptions = res.data;
      })
      .catch((err) => {
        console.log("获取数据失败" + err);
      });
    axios
      .get("http://127.0.0.1/clublist_yang/all")
      .then((res) => {
        console.log(res.data);
        this.cluboptions = res.data;
      })
      .catch((err) => {
        console.log("获取数据失败" + err);
      });
  },
  data() {
    return {
      searchnumber: "",
      searchclass: "",
      selectclass: "",
      classoptions: "",
      cluboptions: "",
      deleteid: "",
      memberlist: [],
      tableData: [],
      editFormVisible: false,
      dialogFormVisible: false,
      form: {
        id: "",
        name: "",
        class: "",
        number: "",
        password: "",
        club: "",
      },
      editform: {
        id: "",
        name: "",
        class: "",
        number: "",
        password: "",
        club: "",
      },
      formLabelWidth: "80px",
    };
  },
  methods: {
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editform = row;
    },
    handleDelete(index, row) {
      this.deleteid = row.id;
      this.del();
    },
    formatter(row, column) {
      console.log(row, column);
      return row.address;
    },
    searchnum() {
      axios
        .get("http://127.0.0.1/memberlist_yang/get", {
          params: {
            number: this.searchnumber,
            class: this.searchclass,
          },
        })
        .then((res) => {
          // console.log(res.data[0]);
          this.memberlist = res.data;
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
    clear() {
      this.searchnumber = "";
      this.all();
    },
    addClub() {
      if (this.form.name == "" || this.form.class == "" || this.form.number == "" || this.form.password == "") {
        alert("请填写完整信息(姓名、班级、学号、密码)后再点击确定");
      } else {
        this.dialogFormVisible = false;
        this.form.id = uuidv4();
        this.add();
        this.form = {
          id: "",
          name: "",
          class: "",
          number: "",
          password: "",
          club: "",
        };
      }
    },
    editClub() {
      // console.log(this.editform);
      if (this.editform.name == "" || this.editform.class == "" || this.editform.number == "" || this.editform.password == "") {
        alert("请填写完整信息(姓名、班级、学号、密码)后再点击确定");
      } else {
        this.update();
      this.editFormVisible = false;
      }
      
    },
    all() {
      //查找info表全部数据
      axios
        .get("http://127.0.0.1/memberlist_yang/all")
        .then((res) => {
          // console.log(res.data);
          this.memberlist = res.data;
          // console.log(this.memberlist);
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
    add() {
      //添加操作
      axios
        .get("http://127.0.0.1/memberlist_yang/add", {
          params: {
            id: this.form.id.trim(),
            name: this.form.name.trim(),
            class: this.form.class.trim(),
            number: this.form.number.trim(),
            password: this.form.password.trim(),
            club: this.form.club.trim(),
          },
        })
        .then((res) => {
          console.log(res);
          this.all();
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
    del() {
      //删除操作
      axios
        .get("http://127.0.0.1/memberlist_yang/del", {
          params: {
            id: this.deleteid,
          },
        })
        .then((res) => {
          console.log(res.data);
          this.all();
        })
        .catch((err) => {
          console.log("操作失败" + err);
        });
    },
    update() {
      //修改操作
      axios
        .get("http://127.0.0.1/memberlist_yang/update", {
          params: {
            id: this.editform.id.trim(),
            name: this.editform.name.trim(),
            class: this.editform.class.trim(),
            number: this.editform.number.trim(),
            password: this.editform.password.trim(),
            club: this.editform.club.trim(),
          },
        })
        .then((res) => {
          console.log(res);
          this.all();
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
  },
};
</script>

<style scoped>
.header {
  height: 56px;
  width: 100%;
  background-color: #1e874c;
}
.add {
  margin-top: 10px;
  margin-left: 20px;
}
.search {
  position: absolute;
  top: 10px;
  right: 200px;
}
.searchbutton {
  position: absolute;
  top: 0;
  right: -70px;
}
.clear {
  position: absolute;
  top: 0;
  right: -140px;
}
.inputnum,
.inputclass {
  height: 10px;
  width: 150px;
  margin-left: 20px;
}
</style>
